package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.common.Result;
import com.example.common.ResultStatus;
import com.example.pojo.NewPhoto;
import com.example.pojo.OldPhoto;
import com.example.service.NewPhotoService;
import com.example.service.OldPhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class PhotoController {
    OldPhotoService oldPhotoService;
    NewPhotoService newPhotoService;

    @Autowired
    public PhotoController(NewPhotoService newPhotoService,OldPhotoService oldPhotoService) {
        this.newPhotoService = newPhotoService;
        this.oldPhotoService=oldPhotoService;
    }

    @RequestMapping("/photo/getphotos")
    public String getAllPhoto(JSONObject json){
        List<NewPhoto> list= (ArrayList<NewPhoto>)newPhotoService.findAllPhoto(json);
        return JSON.toJSONString(list);
    }

    /**
     *
     * @param json(
     *     id:用户id,
     *     photoType:图片类型
     * )
     * @param file
     */
    @RequestMapping("/photo/transform")
    public String transfrom(JSONObject json, MultipartFile file) throws IOException {
            String photoPath= null;//将上传的图片上传到存储空间
            photoPath = (String) oldPhotoService.uploadPath(file);
            oldPhotoService.addPhoto(json,photoPath);//将上传的图片写入数据库
            String photoType=json.getString("photoType");//获取图片类型
            String path= (String)newPhotoService.transform(file);//转化成骰子图
            String url=newPhotoService.uploadPath(path);//上传骰子图
            NewPhoto newPhoto= (NewPhoto) newPhotoService.addNewPhoto(url,photoType);//插入数据库
            if(photoPath!=null){
                    return new Result(ResultStatus.SUCCESS,newPhoto).toJson();
            }else {
                return  ResultStatus.ERROR.toString();
            }
    }

    /**
     * 删除图片
     */
    @RequestMapping("/index")
    public void deletePhoto(JSONObject json){

    }


}
